package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.Querycourse;
import com.atguigu.eduservice.entity.vo.Queryteacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-27
 */
@Api(description = "课程CRUD")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")

public class EduCourseController {
@Autowired
private EduCourseService eduCourseService;

  @PostMapping("savecourseinfo")
    public R savecourseinfo(@RequestBody CourseInfoVo courseInfoVo){
      String id=eduCourseService.saveCourseInfo(courseInfoVo);
      return R.ok().data("courseid",id);
  }
  //根据课程id查询课程基本信息
  @GetMapping("getCourseInfo/{courseId}")
  public R getCourseInfo(@PathVariable String courseId) {
    CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
    return R.ok().data("courseInfoVo",courseInfoVo);
  }

  //修改课程信息
  @CacheEvict(value = "course",key = "'course'")
  @PostMapping("updateCourseInfo")
  public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
    eduCourseService.updateCourseInfo(courseInfoVo);
    return R.ok();
  }

  //根据课程id查询课程确认信息
  @GetMapping("getPublishCourseInfo/{id}")
  public R getPublishCourseInfo(@PathVariable String id) {
    CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(id);
    return R.ok().data("publishCourse",coursePublishVo);
  }


  //发布课程修改状态
  @PostMapping("PublishCourse/{id}")
  public R PublishCourse(@PathVariable String id) {
    EduCourse eduCourse = new EduCourse();
    eduCourse.setId(id);
    eduCourse.setStatus("Normal");//设置课程发布状态
    eduCourseService.updateById(eduCourse);
    return R.ok();

  }
  //查询课程列表
  @PostMapping("getallcourse/{page}/{limit}")
  public R getallcourse( @ApiParam(name = "page", value = "当前页码", required = true)
                           @PathVariable long page,
                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                           @PathVariable long limit,
                         @RequestBody(required = false) Querycourse querycourse){

    Page<EduCourse> pageParam = new Page<>(page, limit);

    QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
    //多条件查询--wrapper
    String title = querycourse.getTitle();
    String status = querycourse.getStatus();
    String begin = querycourse.getBegin();
    String end = querycourse.getEnd();

    if (!StringUtils.isEmpty(title)) {
      wrapper.like("title",title);
    }
    if (!StringUtils.isEmpty(status)) {
      wrapper.eq("status",status);
    }
    if (!StringUtils.isEmpty(begin)) {
      wrapper.ge("gmt_create", begin);
    }
    if (!StringUtils.isEmpty(end)) {
      wrapper.le("gmt_create", end);
    }
    wrapper.orderByDesc("gmt_create");
    eduCourseService.page(pageParam, wrapper);
    //集合
    List<EduCourse> records = pageParam.getRecords();
    //总记录数
    long total = pageParam.getTotal();
    return R.ok().data("total", total).data("rows", records);

  }

  //删除课程
  @DeleteMapping("{courseId}")
  public R deleteCourse(@PathVariable String courseId) {
    eduCourseService.removeCourse(courseId);
    return R.ok();
  }

  //查询课程

  @GetMapping("searchcourse/{name}")
  public R searchcourse(@PathVariable String name){
    List<EduCourse> courseList = eduCourseService.list(new QueryWrapper<EduCourse>().like("title", name.trim()));
    Map<String, Object> map = new HashMap<>();
    map.put("items", courseList);
    map.put("total", courseList.size());
    return R.ok().data(map);
  }




}

